﻿using Microsoft.SharePoint;
using Navigant.Infrastructure.Cases;
using Navigant.Model.PreservationNotices;
using Navigant.Infrastructure.DomainBase;

namespace Navigant.Infrastructure.Repositories
{
    public class SharePointPreservationNoticeTemplateRepository : SharePointRepositoryBase<PreservationNoticeTemplate>, IPreservationNoticeTemplateRepository
    {
        #region Public Constructors

        public SharePointPreservationNoticeTemplateRepository(ICase currentCase)
            : this(currentCase, null)
        {
        }

        public SharePointPreservationNoticeTemplateRepository(ICase currentCase, IUnitOfWork unitOfWork)
            : base(currentCase, unitOfWork)
        {
        }

        #endregion

        #region Protected Methods

        protected override string GetEntityName()
        {
            return "Preservation Notices";
        }

        protected override int PersistNewItem(PreservationNoticeTemplate item)
        {
            using (SPSite site = new SPSite(item.ParentCase.ClientPortalId))
            {
                using (SPWeb web = site.OpenWeb(item.ParentCase.Id))
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists[this.EntityName];
                    SPListItem listItem = list.Items.Add();
                    this.UpdatePreservationNoticeTemplateListItem(listItem, item);
                    return listItem.ID;
                }
            }
        }

        protected override void PersistUpdatedItem(PreservationNoticeTemplate item)
        {
            using (SPSite site = new SPSite(item.ParentCase.ClientPortalId))
            {
                using (SPWeb web = site.OpenWeb(item.ParentCase.Id))
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists[this.EntityName];
                    SPListItem listItem = list.Items.GetItemById(item.Id);
                    this.UpdatePreservationNoticeTemplateListItem(listItem, item);
                }
            }
        }

        protected override void BuildChildCallbacks()
        {
            base.BuildChildCallbacks();
            this.ChildCallbacks.Add("attachments",
                delegate(PreservationNoticeTemplate template, object childEntityKeyValue)
                {
                    this.AppendAttachments(template);
                });
        }

        #endregion

        #region Private Methods

        private void AppendAttachments(PreservationNoticeTemplate template)
        {
            SPFile file = null;
            Attachment attachment = null;

            using (SPSite site = new SPSite(template.ParentCase.ClientPortalId))
            {
                using (SPWeb web = site.OpenWeb(template.ParentCase.Id))
                {
                    SPList list = web.Lists[this.EntityName];
                    SPListItem templateItem = list.GetItemById(template.Id);
                    if (templateItem.Attachments != null)
                    {
                        foreach (string fileName in templateItem.Attachments)
                        {
                            file = web.GetFile(templateItem.Attachments.UrlPrefix + fileName);
                            attachment = new Attachment(file.OpenBinary(SPOpenBinaryOptions.None), file.Name, file.Url);
                            template.AddAttachment(attachment);
                        }                        
                    }
                }
            }
        }

        private void UpdatePreservationNoticeTemplateListItem(SPListItem listItem, PreservationNoticeTemplate item)
        {
            listItem["Name"] = item.Name;
            listItem["Message Subject"] = item.Subject;
            listItem["Message Body"] = item.Body;
            
            // Commented Code to remove status field from Preservation Notice
            //listItem["Status"] = item.Status;

            foreach (Attachment attachment in item.Attachments)
            {
                listItem.Attachments.Add(attachment.FileName, attachment.Content);
            }

            listItem.Update();
        }

        #endregion
    }
}
